<?php
class admin_list_contentController extends adminController {

	public function index() {

        $fid=intval($_GET['fid']);
        $this->alert_str($fid,'int');
        $this->form_info=model('form')->info($fid);
        $this->field_list=model('form_field_data')->field_list($fid);
		$this->show();
	}

    //AJAX获取内容列表
    public function ajax_content_list()
    {
        $fid=intval($_POST['fid']);
        //分页参数
        $listRows=20;
        $page = intval($_POST['page']);
        $limit_start = ($page - 1) * $listRows;
        $limit = $limit_start . ',' . $listRows;

        //表单信息
        $form_info=model('form')->info($fid);
        $field_list=model('form_field_data')->field_list($fid);

        //列表信息
        $list=model('form_field_data')->form_list($form_info['table'],$limit,$form_info['order']);
        $count=model('form_field_data')->form_count($form_info['table']);

        //总页数
        $countpage=ceil($count/$listRows);

        if(!empty($list)&&!empty($field_list)){
            $html='';
            foreach ($list as $vo) {
                $html.='<tr id="del_'.$vo['id'].'">
                    <td><input name="id[]" type="checkbox" id="id[]" value="'.$vo['id'].'" ></td>
                    <td>'.$vo['id'].'</td>';
                foreach ($field_list as $model) {
                    $html.='<td>';
                    $html.=api('expand_model','get_list_model',array('type'=>$model['type'],'str'=>$vo[$model['field']],'config'=>$model['config']));
                    $html.='</td>';
                }
                $html.='
                    <td>
                    <a class="operating" href="'.url('admin_list_content/edit',array('id' => $vo['id'],'fid'=>$form_info['id'])).'"  title="编辑表单内容" ><span class="icon">&#xf040;</span> 修改</a>
                    <a class="operating" href="javascript:void(0);" onClick="del(\''.$vo['id'].'\',this)"><span class="icon">&#xf014;</span> 删除</a>
                    </td>
                ';
            }
            $status=1;
        }else{
            $html='<tr><td colspan="8"><center>暂无查找到内容<center></td></tr>';
            $status=0;
        }
        ;

        @header("Content-type:text/html");
        echo json_encode(array('status' => $status, 'info' => $html ,'countpage'=>$countpage));
    }

    public function data_check($data)
    {
        //获取所有字段
        $field_list=model('form_field')->field_list($data['fid']);
        if(empty($field_list)){
            $this->msg('未发现表单字段！',0);
        }
        foreach ($field_list as $value) {
            $verification=unserialize($value['verification']);
            if(!empty($verification)){
                if(empty($data[$value['field']])){
                    $this->msg($value['name'].'不能为空！',0);
                }
            }
            $data[$value['field']]=api('expand_model','field_in',array('data'=>$data,'type'=>$value['type'],'field'=>$value['field']));
        }
        return $data;
    }

    //表单添加
    public function add() {
        $fid=$_GET['fid'];
        $this->alert_str($fid,'int');
        $this->field_list=model('form_field')->field_list($fid);
        $this->form_info=model('form')->info($fid);
        $this->action_name='添加';
        $this->action='add';
        $this->show('admin_list_content/info'); 
    }

    //表单数据处理
    public function add_save() {
        $this->alert_str($_POST['fid'],'int',true);
        $data=$this->data_check($_POST);
        //处理完毕后交由模型处理数据
        $id=model('form_field_data')->add($data);
        api('editor','relation',array('table'=>'form','file_id'=>$data['file_id'],'id'=>$id));
        $this->msg('添加成功！',1);

    }

    //表单编辑
    public function edit() {
        $fid=$_GET['fid'];
        $id=$_GET['id'];
        $this->alert_str($fid,'int');
        $this->alert_str($id,'int');
        $this->field_list=model('form_field')->field_list($fid);
        $this->form_info=model('form')->info($fid);
        $this->info=model('form_field_data')->info($id,$this->form_info['table']);
        $this->file_id=api('editor','get_relation',array('table'=>'form','id'=>$id));
        $this->action_name='编辑';
        $this->action='edit';
        $this->show('admin_list_content/info');
    }

    //内容编辑数据处理
    public function edit_save() {
        $this->alert_str($_POST['fid'],'int',true);
        $this->alert_str($_POST['id'],'int',true);
        $data=$this->data_check($_POST);
        //模型处理数据
        model('form_field_data')->edit($data);
        api('editor','relation',array('table'=>'form','file_id'=>$data['file_id'],'id'=>$data['id']));
        $this->msg('编辑成功！',1);

    }

    //删除
    public function del() {
        $id=$_POST['id'];
        $fid=$_POST['fid'];
        $this->alert_str($_POST['fid'],'int',true);
        $this->alert_str($_POST['id'],'int',true);
        model('form_field_data')->del($id,$fid);
        api('editor','del_file',array('table'=>'form','id'=>$id));
        $this->msg('内容删除成功！',1);
    }

    //批量操作
    public function batch(){
        if(empty($_POST['status'])||empty($_POST['id'])){
            $this->msg('操作无效！',0);
        }
        $id_array=substr($_POST['id'],0,-1);
        $id_array=explode(',', $id_array);
        if(empty($id_array)){
            $this->msg('请先选择内容！',0);
        }
        switch ($_POST['status']) {
            case '1':
                //删除
                $fid=intval($_POST['fid']);
                if(empty($fid)){
                    $this->msg('通讯错误，请刷新本页！',0);
                }
                foreach ($id_array as $value) {
                     model('form_field_data')->del($value,$fid);
                     api('editor','del_file',array('table'=>'form','id'=>$value));
                }
                break;
        }
        $this->msg('操作执行完毕！',1);

    }


}